package dolf.objects.weapons.ShrinkBall;

import javax.swing.ImageIcon;
import dolf.game.Level;
import dolf.objects.ImageStore;
import dolf.objects.weapons.*;
import dolf.game.Player;
import dolf.objects.WorldObject;
import dolf.objects.Vector;
import dolf.window.SystemMessagesHandler;

public class ShrinkBall extends WorldObject implements Weapon, java.io.Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public static final int Price = 50;
	public static final String Name = SystemMessagesHandler.getInstance().getMessages("dolf.objects.weapons.ShrinkBall.lang.lang").getString("name");
	public static final String WeaponType = SystemMessagesHandler.getInstance().getMessages("dolf.objects.weapons.ShrinkBall.lang.lang").getString("type");
	public static final String Description = SystemMessagesHandler.getInstance().getMessages("dolf.objects.weapons.ShrinkBall.lang.lang").getString("description");
	public static final javax.swing.ImageIcon Icon = ImageStore.getInstance().getIcon("dolf/objects/weapons/ShrinkBall/gfx/icon.png");
	
	private int preRadius;
	private final int SIZE = 3;
	private Player pl;
	private boolean inititalized = false;
	private boolean shrinking = false;
	private boolean growing = false;
	private boolean smale = false;
	private boolean used = false;
	private Vector lastMotion;
	

	public ShrinkBall(){
		super(false);
	}
	public void initialize(Player _pl){
		if(this.inititalized){
			return;
		}
		this.inititalized = true;
		shrinking = false;
		growing = false;
		smale = false;
		this.pl = _pl;
		this.preRadius = pl.getBall().getRadius();
		this.used = false;
		pl.setWeaponState();
	
	}
	
	public void deInitialize(){
		this.inititalized = false;
		this.pl = null;
	}
	
	public void execute() {
		if(!this.shrinking && !this.growing && !this.smale && Level.getInstance().mousePressed()){
			this.shrinking = true;
			Level.getInstance().registerAnimation(this);
			this.used = true;
			pl.setWeaponState();
		}
		if(this.shrinking && !this.growing){
			pl.getBall().setRadius(pl.getBall().getRadius()-1);
			if(pl.getBall().getRadius() == this.SIZE){
				this.shrinking = false;
				this.smale = true;
			}
		}
		if(this.smale && !this.growing){
			this.lastMotion = pl.getBall().getMotion().getCopy();
		}
		if(this.growing){
			if(pl.getBall().getRadius() < this.preRadius){
				pl.getBall().setRadius(pl.getBall().getRadius()+1);
				for(int i = 0; i < Level.getInstance().getObjects().size();i++){
					WorldObject obj = Level.getInstance().getObjects().get(i);
					pl.getBall().setMovable(false);
					if(obj != pl.getBall() && obj.isVisible() && obj.isCollitabe() &&this.pl.getBall().kollision(obj)==0 ){
						pl.getBall().setMovable(true);
						pl.getBall().setRadius(pl.getBall().getRadius()-1);
						pl.getBall().setMotion(this.lastMotion.setLength(0.7));
						obj.newMotion(pl.getBall());
						break;
						//pl.getBall().startMove(Level.getInstance().getObjects());
					}
				}
				pl.getBall().setMovable(true);
			}
		}
	}

	public ImageIcon getIcon() {
		return Icon;//ImageStore.getInstance().getIcon("dolf/objects/weapons/ShrinkBall/gfx/icon.png");	

	}

	public String getName() {
		return Name;
		/*if (messages == null) loadProperties();
		return messages.getString("name");*/
	}
	
	public String getDescription() {
		return Description;
		/*if (messages == null) loadProperties();
		return messages.getString("description");*/
	}
	public String getType(){
		return WeaponType;
	}

	public int getPrice() {
		return Price;
	}

	public void remove() {
		if(!this.growing){
			this.growing = true;
		}
		if(pl.isInPocket() || !pl.getBall().isVisible() || pl.getBall().getRadius()==this.preRadius){
			pl.getBall().setRadius(this.preRadius);
			Level.getInstance().removeAnimation(this);
			pl.removeWeapon(this);
			this.pl.setAktWeapon(null);
		}
	}
	public Player getPlayer(){
		return this.pl;
	}
	public double getState(){
		if(used){
			return 0;
		} else {
			return 1;
		}
	}
}
